package com.origin.onlinesys.web.controllers.modules.account;

import com.alibaba.fastjson.JSON;
import com.origin.onlinesys.common.controllers.BaseController;
import com.origin.onlinesys.domain.modules.interviewsys.Permission;
import com.origin.onlinesys.domain.modules.interviewsys.Role;
import com.origin.onlinesys.service.modules.user.impl.PermissionServiceImplShiro;
import com.origin.onlinesys.service.modules.user.impl.RolesServiceImpl;
import com.origin.onlinesys.web.controllers.modules.login.UserLoginController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Administrator on 2015/3/7 0007.
 */
@Controller
@RequestMapping(value = "management/role")
public class RoleController extends BaseController {
    private static final Logger log = Logger.getLogger(RoleController.class);
    @Autowired
    private RolesServiceImpl service;

    @Autowired
    private PermissionServiceImplShiro perService;

    @RequestMapping(value = "/add")
    public @ResponseBody
    int addRole(@ModelAttribute Role role,@RequestParam("menuId[]") ArrayList<String> menuId){
        role.setState("1");
        service.save(role);
        List<Map> list = new ArrayList();Map map = null;
        for(String id:menuId){
            map = new HashMap();
            map.put("rId",role.getId());map.put("pId",id);
            list.add(map);
        }
        service.addRolePermission(list);

        //添加角色的静态页面菜单
        this.writerHtml("templetNav.html","menu",
                service.selectRoleMenu(role.getId()),"navRole"+role.getId()+".html");
        return 1;
    }


    @RequestMapping(value = "/findItem")
    public @ResponseBody
    List<Role> findItem(){
        return service.selectItem();
    }

    @RequestMapping(value = "/findInfo")
    public @ResponseBody
    Map findInfo(String id){
        Permission permission = new Permission();permission.setState("1");
        List<Permission> list = perService.selectByWhere(permission);
        List<String> pId = service.selectPIdByRId(id);
        for(Permission bean:list){
            for(int i = 0;i<pId.size();i++){
                if(pId.get(i).equals(bean.getId())){
                    bean.setCheck(true);break;
                }
            }
        }
        Map map = new HashMap();
        map.put("info",service.selectById(id));
        map.put("permission",list);
        return map;
    }

    @RequestMapping(value = "/update")
    public @ResponseBody
    int update(@ModelAttribute Role role,@RequestParam("menuId[]")ArrayList<String> menuId){
        service.update(role);
        service.delRolePermission(role.getId());

        List<Map> list = new ArrayList();Map map = null;
        for(String id:menuId){
            map = new HashMap();map.put("rId",role.getId());map.put("pId",id);
            list.add(map);
        }
        service.addRolePermission(list);

        //更新角色的静态页面菜单
        this.writerHtml("templetNav.html","menu",
                service.selectRoleMenu(role.getId()),"navRole"+role.getId()+".html");
        return 1;
    }



    @RequestMapping(value = "/del")
    public @ResponseBody
    int delRole(String id){
        Role role = new Role();
        role.setId(id);
        return service.delete(role);
    }
    @RequestMapping(value = "/findAll")
    public @ResponseBody
    Collection<Role> findAll(){
        return service.selectAllAsObj();
    }

    @RequestMapping(value = "/findByUserId")
    public @ResponseBody
    Collection<Role> findByUserId(){
        String userId = "1";
        return service.selectByUserIdAsObj(userId);
    }
}
